/**
 * Created by hejiahui on 2018/6/23.
 */

var db =  require('../connect')

const Schema = db.Schema

//account对应的字段
const userSchema = new Schema(
    {
        name: { type: String, maxlength: 15 },
        password: { type: String, maxlength: 50 },
        HandLockPass: { type: String, minLength : 4 },
        headImgUrl : { type : String },
        create_date: { type: Date },
        update_date: { type: Date },

    },
    {
        versionKey: false
    }
)
//当account执行save()前，执行该代码片段，有点类似于中间件(这个方法内容仅仅是介绍pre()的使用方法)
userSchema.pre('save', function(next) {
    const currentDate = new Date()
    if (!this.create_date) {
        this.create_date = currentDate
    } else {
        this.update_date = currentDate
    }
    next()
})
//定义模型的方法
userSchema.statics.newUser = async function (registerObj){
    let res = await this.create(registerObj)
    return res
}

userSchema.set('collection', 'user')
const User = db.model('user', userSchema)

module.exports = User